// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init({
    env:'a-1gb4haz213d919e3'
})

// 云函数入口函数
exports.main = async (event, context) => {
    const wxContext = cloud.getWXContext()
    const db=cloud.database()
    const studentCollection=db.collection('student')
    let openid=wxContext.OPENID
    console.log('event:',event)
    let {sn,name,nickName,avatarUrl}=event
    console.log(sn,name,nickName,avatarUrl)
    let res=await studentCollection.where({
        sn:sn,
        name:name
    }).get()

    let user={}
    let reg='ok'
    let errMsg=''

    if(res.data.length==0){
        reg='错误'
        errMsg='没有匹配的学号姓名，请核实'
    }else{
        let {_id}=res.data[0]
        let student=res.data[0]
        if(!res.data[0].nickName){
            res = await studentCollection.doc(_id).update({
                data:{
                    nickName,avatarUrl,openid
                }
            })
            user={...student,nickName,avatarUrl,openid}
        }else{
            reg='错误'
            errMsg='学号:'+sn+'已被昵称为\"'+nickName+'\"的微信号绑定'
        }
    }

    return {
        reg,errMsg,user
    }
}